package com.zjs.jsp.ediorder.web;

import com.zjs.jsp.domain.axd.AxdOrderRootData;
import com.zjs.jsp.domain.axd.out.AxdOrderGps;
import com.zjs.jsp.domain.config.WebHelpService;
import com.zjs.jsp.domain.stats.jsp_locationMsg;
import com.zjs.jsp.ediorder.domain.mongodb.jsp_Status;
import com.zjs.jsp.ediorder.service.BaseOrderQueryService;
import com.zjs.jsp.ediorder.service.mongodb.jsp_locationMagRepository;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李文
 * @create 2017-10-13 13:54
 **/
@RestController
public class QueryOrderGPSApiController extends BaseOrderQueryService
{
    private static final Logger LOGGER_FACTORY = LoggerFactory.getLogger(QueryOrderGPSApiController.class);


    @Autowired
    jsp_locationMagRepository j;


    @ApiOperation(value = "查询配送员位置 ", notes = "查询配送员实时位置  通过订单号  ")
    @ApiImplicitParam(name = "root", value = "数据", required = true, dataType = "AxdOrderRootData", paramType = "body")
    @PostMapping("/order/query/gps/{uuid}")
    public String orderInquiry(@PathVariable String uuid, @RequestBody AxdOrderRootData root, HttpServletRequest request) {
        LOGGER_FACTORY.info(uuid + " 接受的数据是 " + root + "  " + WebHelpService.getParameterMap(request));
        try {
            boolean b = root.getPartner_order_code().size() > 10;
            if (b) {
                LOGGER_FACTORY.error(uuid + "  订单号数量大于1 ：", new Exception("订单号数量大于1 "));
                return WebHelpService.getString(40000, "订单号数量大于1");
            }
        } catch (Exception e) {
            LOGGER_FACTORY.error(uuid + "  获取请求订单号错误 ：" + e.getMessage(), e);
            return WebHelpService.getString(40000, "获取请求订单号错误");
        }
        String d = this.queryOrder(root, uuid);
        LOGGER_FACTORY.info(uuid + "  响应数据 ：" + d);
        return d;
    }

    @Override
    public void getDate(List<Object> data, List<jsp_Status> s) throws Exception {
        List<AxdOrderGps> gps = new ArrayList<>();
        if (s != null && s.size() > 0) {
            jsp_Status status;
            if (s.size() == 1) {
                status = s.get(0);
            } else {
                status = s.get(s.size() - 1);
            }
            AxdOrderGps g = new AxdOrderGps();
            g.setPartner_order_code(status.getPoc());
            g.setRider_code(status.getRc());
            g.setRider_name(status.getRn());
            jsp_locationMsg jspLocationmsg = j.findByUserIDOrderByTimeDesc(status.getRc());
            if (jspLocationmsg == null) {
                throw new Exception(" 并未查询到 派送员 GPS 信息");
            }
            g.setRider_lat(new BigDecimal(jspLocationmsg.getLat()));
            g.setRider_lng(new BigDecimal(jspLocationmsg.getLon()));
            gps.add(g);
        }
        data.addAll(gps);
    }
}
